// 测试环境配置文件
import { config } from '@vue/test-utils'

// Mock uni-app API
global.uni = {
  navigateTo: jest.fn(),
  redirectTo: jest.fn(),
  switchTab: jest.fn(),
  navigateBack: jest.fn(),
  showToast: jest.fn(),
  showModal: jest.fn(),
  showLoading: jest.fn(),
  hideLoading: jest.fn(),
  vibrateShort: jest.fn(),
  vibrateLong: jest.fn(),
  stopPullDownRefresh: jest.fn(),
  startPullDownRefresh: jest.fn(),
  getStorageSync: jest.fn(),
  setStorageSync: jest.fn(),
  removeStorageSync: jest.fn(),
  clearStorageSync: jest.fn(),
  getSystemInfoSync: jest.fn(() => ({
    platform: 'mp-weixin',
    version: '1.0.0',
    SDKVersion: '2.0.0'
  })),
  uploadFile: jest.fn(),
  downloadFile: jest.fn(),
  request: jest.fn(),
  connectSocket: jest.fn(),
  onSocketOpen: jest.fn(),
  onSocketMessage: jest.fn(),
  onSocketClose: jest.fn(),
  onSocketError: jest.fn(),
  sendSocketMessage: jest.fn(),
  closeSocket: jest.fn()
}

// Mock uView组件
config.stubs = {
  'u-search': true,
  'u-icon': true,
  'u-picker': true,
  'u-loading-icon': true,
  'u-button': true,
  'u-input': true,
  'u-textarea': true,
  'u-switch': true,
  'u-radio': true,
  'u-checkbox': true,
  'u-upload': true,
  'u-image': true,
  'u-avatar': true,
  'u-badge': true,
  'u-tag': true,
  'u-rate': true,
  'u-slider': true,
  'u-popup': true,
  'u-modal': true,
  'u-toast': true,
  'u-loading': true,
  'u-actionsheet': true,
  'u-keyboard': true,
  'u-numberbox': true,
  'u-countdown': true,
  'u-circle-progress': true,
  'u-line-progress': true,
  'u-charts': true,
  'u-verification-code': true,
  'u-read-more': true,
  'u-divider': true,
  'u-grid': true,
  'u-grid-item': true,
  'u-index-list': true,
  'u-index-item': true,
  'u-sticky': true,
  'u-steps': true,
  'u-steps-item': true,
  'u-swipe-action': true,
  'u-swipe-action-item': true,
  'u-collapse': true,
  'u-collapse-item': true,
  'u-navbar': true,
  'u-tabbar': true,
  'u-tabbar-item': true,
  'u-tabs': true,
  'u-tab': true,
  'u-subsection': true,
  'u-top-tips': true,
  'u-alert': true,
  'u-empty': true,
  'u-gap': true,
  'u-cell': true,
  'u-cell-item': true,
  'u-field': true,
  'u-form': true,
  'u-form-item': true
}

// Mock global methods
global.getCurrentPages = jest.fn(() => [])
global.getApp = jest.fn(() => ({
  globalData: {}
}))

// Suppress console warnings in tests
global.console = {
  ...console,
  warn: jest.fn(),
  error: jest.fn()
}

// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
  constructor() {}
  disconnect() {}
  observe() {}
  unobserve() {}
}

// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
  constructor() {}
  disconnect() {}
  observe() {}
  unobserve() {}
}

// Mock matchMedia
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: jest.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(),
    removeListener: jest.fn(),
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn(),
  })),
})

// Mock localStorage
const localStorageMock = {
  getItem: jest.fn(),
  setItem: jest.fn(),
  removeItem: jest.fn(),
  clear: jest.fn(),
}
global.localStorage = localStorageMock

// Mock sessionStorage
const sessionStorageMock = {
  getItem: jest.fn(),
  setItem: jest.fn(),
  removeItem: jest.fn(),
  clear: jest.fn(),
}
global.sessionStorage = sessionStorageMock

// Mock URL
global.URL = class URL {
  constructor(url) {
    this.href = url
    this.protocol = 'https:'
    this.host = 'example.com'
    this.pathname = '/path'
    this.search = '?query=test'
    this.hash = '#hash'
  }
}

// Mock fetch
global.fetch = jest.fn(() =>
  Promise.resolve({
    ok: true,
    status: 200,
    json: () => Promise.resolve({}),
    text: () => Promise.resolve(''),
    blob: () => Promise.resolve(new Blob()),
  })
)

// Set up default timeouts
jest.setTimeout(10000)

// Clean up after each test
afterEach(() => {
  jest.clearAllMocks()
  jest.clearAllTimers()
})